<?php

/***************************************************************\
* FILE:		sys/pages/movereplies.inc
* SECTION:	pages > item management
* FUNCTION:	moves replies to a new or other item
* USES:	
* INCLUDES:		
* PRODUCES:	
* LAUNCHES:	
\***************************************************************/

global $no_menu;
$no_menu = 1;

/**
 * adds extra access checks, dependent on page state
 */
function page_inc_check_access() {
	check_access("READ");
	if (!has_access("MODERATE") and !has_access("ADMIN"))
		errorpage(5);
}


/**
 * execute page logic
 */
function page_inc_execute() {
	global $PAGE;
	$CONTENT = array();
	
	// check access: 
	// 	- check whether user has moderator rights AND 
	//	- check whether supplied ids belong in this directory

	$CONTENT['title'] = "Reacties verplaatsen";

	$sql = db_query_select(array(array(), 'dir_id'), array("(POST INNER JOIN ITEM_IN ON POST.item_id = ITEM_IN.item_id) INNER JOIN SUB_IN ON ITEM_IN.sub_id = SUB_IN.sub_id", "DUMMY"), "post_id IN ({$_GET['ids']}) AND ITEM_IN.item_id = {$PAGE['id']}");
	$sqlresult = db_do_query($sql);
	$ok = 0;
	while ($r = db_fetch_array($sqlresult)) {
		if ($r['dir_id'] == $PAGE['directory_tree'][$PAGE['dir_depth']-1]['dir_id'])
			$ok = 1;
	}
	if (!$ok)
		errorpage(11);
	if (!has_access("MODERATE") and !has_access("ADMIN"))
		errorpage(5);

	if ($_POST['submitted']) {
	
		if ($_POST['dir_id'] and $_POST['sub_id'] and $_POST['item_id']) {
		
			include_once('sys/inputcheck.inc');
		
			$ok = 1;
			if ($_POST['item_id'] == "NEW") {
				$ok = (check_exists($_POST['new_item'], "titel item") and $ok) ? 1 : 0;
				$ok = (check_tags($_POST['new_item'], "titel item") and $ok) ? 1 : 0;
			}
			if ($_POST['sub_id'] == "NEW") {
				$ok = (check_exists($_POST['new_subset'], "titel categorie") and $ok) ? 1 : 0;
				$ok = (check_tags($_POST['new_subset'], "titel categorie") and $ok) ? 1 : 0;
			}
			

			if ($ok) {

				// subset insertion
				if ($_POST['sub_id'] == "NEW") {
				
					$sql = db_query_insert(array('sub_title'), array(db_sqlize($_POST['new_subset'])), "SUBSET");
					db_do_query($sql);
					$sub_id = db_insert_id();
					
					// in_dir insertion
					$sql = db_query_insert(array('dir_id', 'sub_id'), array($_POST['dir_id'], $sub_id), "SUB_IN");
					db_do_query($sql);
					
				} else {
					$sub_id = $_POST['sub_id'];
				}
			
				// item insertion
				if ($_POST['item_id'] == "NEW") {
				
					// fetch title
					$post = db_fetch_array(db_do_query(db_query_select(array(array('title')), array('POST'), "post_id IN ({$_GET['ids']})", "creation ASC", "0,1")));
					
					// insert item
					$sql = db_query_insert(array("item_type_id", "author", "editor", "title", "info", "hits", "creation", "edit"), array(1, $_SESSION['user_id'], $_SESSION['user_id'], db_sqlize($post['title']), "''", 0, "NOW(14)", "NOW(14)"), "ITEM");
					db_do_query($sql);
					$item_id = db_insert_id();
					
					// in_sub insertion
					$sql = db_query_insert(array('sub_id', 'item_id'), array($sub_id, $item_id), "ITEM_IN");
					db_do_query($sql);
				
				} else {
					$item_id = $_POST['item_id'];
				}
				
				// move posts
				db_do_query(db_query_update(array('creation', 'edit', 'item_id'), array('creation', 'edit', $item_id), "POST", "post_id IN ({$_GET['ids']})"));
				
				// update items post count
				db_do_query(db_query_update(array('creation', 'edit', 'num_posts'), array('creation', 'edit', 'num_posts-' . (substr_count($_GET['ids'], ',')+1)), "ITEM", "item_id = {$PAGE['id']}"));
				db_do_query(db_query_update(array('creation', 'edit', 'num_posts'), array('creation', 'edit', 'num_posts+' . (substr_count($_GET['ids'], ',')+1)), "ITEM", "item_id = {$item_id}"));
		
				endpage("OK, verplaatst" . (($_POST['item_id'] == 'NEW') ? " (pas indien gewenst de titel van het nieuwe topic aan)" : ""));
			}
		}
	}
	
	$inputs = array();
	generate_menu();
	$keys = array();
	$vals = array();
	list($keys, $vals) = generate_list(0, 0, $keys, $vals, $PAGE['menu']);
	$inputs[] = form_input_select("Directory", "dir_id", $keys, $vals, "directory", "", "document.moveform.submit()");

	if ($_POST['dir_id']) {
		$sql = db_query_select(array(array("*"), array()), array("SUBSET", "SUB_IN"), "SUBSET.sub_id = SUB_IN.sub_id AND SUB_IN.dir_id = {$_POST['dir_id']}", "sub_title");
		$sqlresult = db_do_query($sql);
		$keys = array();
		$values = array();
		$default = "";
		if (db_num_rows($sqlresult))
			while ($r = db_fetch_array($sqlresult)) {
				$keys[] = $r['sub_id'];
				$title = $r['sub_title'];
				if (strlen($title) > 30)
					$title = substr($title, 0, 30) . "...";
				$values[] = $title;
			}
		$keys[] = "NEW";
		$values[] = $_POST['new_subset'] ? $_POST['new_subset'] : "NIEUWE SET";
		$inputs[] = form_input_select("Subset", "sub_id", $keys, $values, "selecteer een categorie", $default, "document.moveform.submit()");
		
		if ($_POST['sub_id'] == "NEW")
			$inputs[] = form_input_field("Nieuwe subset", "new_subset");
	}
	
	if ($_POST['sub_id']) {
		if ($_POST['sub_id'] != "NEW") {
			$sql = db_query_select(array(array("*"), array()), array("ITEM", "ITEM_IN"), "ITEM.item_id = ITEM_IN.item_id AND ITEM_IN.sub_id = {$_POST['sub_id']}", "title");
			$sqlresult = db_do_query($sql);
			$keys = array();
			$values = array();
			$default = "";
			if (db_num_rows($sqlresult))
				while ($r = db_fetch_array($sqlresult)) {
					$keys[] = $r['item_id'];
					$title = $r['title'];
					if (strlen($title) > 30)
						$title = substr($title, 0, 30) . "...";
					$values[] = $title;
				}
			$keys[] = "NEW";
			$values[] = $_POST['new_subset'] ? $_POST['new_subset'] : "NIEUW ITEM";
			$inputs[] = form_input_select("Item", "item_id", $keys, $values, "selecteer een item", $default, "document.moveform.submit()");
		}
		
		if (($_POST['item_id'] == "NEW") or ($_POST['sub_id'] == "NEW"))
			$inputs[] = form_input_field("Nieuw topic", "new_item");
		if ($_POST['sub_id'] == "NEW")
			$inputs[] = form_input_hidden("item_id", "NEW");
	}
	
	$CONTENT['form'] = form_create("moveform", array_reverse($inputs), $_SERVER['REQUEST_URI']);

	$PAGE['content'] = $CONTENT;
}

function generate_list($parent, $level, $keys, $vals, $menu) {
		
	$rank = 0;	
	$space = "";
	for ($i = 0; $i < $level; $i++)
		$space .= "&nbsp;&nbsp;&nbsp;&nbsp;";
	while ($id = @array_shift($menu[$parent]['children'])) {
		if ($menu[$id]['access'] != "DENY") {
			array_push($keys, "$id");
			array_push($vals, $space . $menu[$id]['menuname']);
			list($keys, $vals) = generate_list($id, ($level+1), $keys, $vals, $menu);
		}
	}	
	return array($keys, $vals);
}


/**
 * dress up page content using skin stuff
 */
function page_inc_dress() {
	global $PAGE;
	$CONTENT = $PAGE['content'];
	if ($CONTENT['body'])
		print_body($CONTENT['body']);
	if ($CONTENT['form'])
		print_form($CONTENT['form']);
}

?>